<template>
    <div>
        <button type="primary" @click="getToken">申请令牌</button>
        <button type="primary" @click="getSuperToken">申请super令牌</button>
        <button type="primary" @click="pay">支付</button>
        <button type="primary" @click="delivery">发货</button>
        <form @click="formID" report-submit="true">
            <button type="primary" form-type="submit">表单id</button>
        </form>
        <button type="primary" @click="checkSession">检查登录状态</button>
    </div>
</template>

<script>
export default {
    data() {
        return {
            app: getApp(),
            baseUrl: 'http://localhost/ccx_api/public/api/v1'
        }
    },
    methods: {
        getSuperToken() {
            var that = this
            wx.request({
                url: that.baseUrl + '/token/app',
                data: {
                    ac: 'warcraft',
                    se: '777'
                },
                method: 'POST',
                success: function(res) {
                    console.log(res.data);
                    wx.setStorageSync('super_token', res.data.token)
                },
                fail: function() {
                    //
                },
                complete: function() {
                    //
                }
            })
        },
        getToken(){
            var that = this
            wx.login({
                success: function(res) {
                    var code = res.code;
                    console.log('code: ',code);
                    
                    wx.request({
                        url: that.baseUrl + '/token/user',
                        data: {
                            code: code
                        },
                        method: 'POST',
                        success: function (res) {
                            console.log(res.data);
                            wx.setStorageSync('token', res.data.token)
                        },
                        fail: function() {
                            //
                        },
                        complete: function() {
                            //
                        }
                    })
                }
            })
        },
        checkSession() {
            wx.checkSession({
                success: function () {
                    console.log('session success');
                },
                fail: function () {
                    console.log('session fail');
                    
                }
            })
        },
        pay() {
            var token = wx.getStorageSync('token')
            var that = this
            wx.request({
                url: that.baseUrl + '/order',
                header: {
                    token: token
                },
                data: {
                    products: [
                        {
                            dish_id: 1, order_count: 11
                        },
                        {
                            dish_id: 2, order_count: 11
                        },
                        {
                            dish_id: 3, order_count: 11
                        }
                    ]
                },
                method: 'POST',
                success: function (res) {
                    console.log(res.data);
                    /* if (res.data.pass) {
                        wx.setStorageSync('orders_id', res.data.orders_id)
                        that.getPreOrder(token, res.data.orders_id)
                    } else {
                        console.log('订单未创建');
                    } */
                },
            })
        },
        getPreOrder(token, orderID) {
            var that = this
            if (token) {
                wx.request({
                    url: that.baseUrl + '/pay/pre_order?XDEBUG_SESSION_START=12388',
                    method: 'POST',
                    header: {
                        token: token
                    },
                    data: {
                        id: orderID
                    },
                    success: function (res) {
                        var preData = res.data
                        wx.requestPayment({
                            timeStamp: preData.timeStamp.toString(),
                            nonceStr: preData.nonceStr,
                            package: preData.package,
                            signType: preData.signType,
                            paySign: preData.paySign,
                            success: function (res) {
                                console.log(res.data);
                            },
                            fail: function (error) {
                                console.log(error);
                                
                            }
                        })
                    }
                })
            }
        },
        delivery() {
            var that = this
            wx.request({
                url: that.baseUrl + '/order/delivery?XDEBUG_SESSION_START=12388',
                header: {
                    token: wx.setStorageSync('super_token')
                },
                method: 'POST',
                data: {
                    id: 293
                },
                success: function (res) {
                    console.log(res.data);
                    
                }
            })
        },
        formID(event) {
            console.log(event);
            
        },
    }
}
</script>

<style scoped>

</style>